package powscl

import (
	"encoding/json"
	"fmt"
	"github.com/ChimeraCoder/anaconda"
	"log"
	"math"
	"net/url"
	"strconv"
	"strings"
)

const (
	winScl  = 0.9
	lossScl = 0.6
)

var (
	Matches []Match
)

type Team struct {
	TeamNumber      int
	TeamName, Notes string

	Played int

	OffRank, DefRank float64
	TelRank, AutRank float64
	Scaled           ScaledRank

	AvgSumScore  float64
	AvgTeleScore float64
	AvgAutoScore float64
	AvgPenScore  float64

	Strategy []bool

	QP, RP            float64
	Wins, Losses, Tie float64
	WinRate           float64
}

type ScaledRank struct {
	OffRank, DefRank float64
	TelRank, AutRank float64
}

type Match struct {
	RedTeams  []int
	BlueTeams []int
	Number    int

	RScore, BScore int
	RAuto, BAuto   int
	RTele, BTele   int
	RPen, BPen     int
}

func UpdateTeamList(matchList []Match, oldList map[int]Team) map[int]Team {
	newList := make(map[int]Team)
	for q := 0; q < len(matchList); q++ {
		for t := 0; t < len(matchList[q].RedTeams); t++ {
			newList[matchList[q].RedTeams[t]] = oldList[matchList[q].RedTeams[t]]
		}
		for t := 0; t < len(matchList[q].BlueTeams); t++ {
			newList[matchList[q].BlueTeams[t]] = oldList[matchList[q].BlueTeams[t]]
		}
	}
	return newList
}

func PrelimCalc(matchList []Match, teamList map[int]Team) map[int]Team {
	type TeamTempDat struct {
		AutoScore float64
		TeleScore float64

		Penalties float64
		Played    float64

		QP, RP         float64
		Win, Loss, Tie float64
	}
	teamDatMap := make(map[int]TeamTempDat)

	for q := 0; q < len(matchList); q++ {
		var match = matchList[q]
		for t := 0; t < 3; t++ {
			var rTeamNum = matchList[q].RedTeams[t]
			var bTeamNum = matchList[q].BlueTeams[t]
			var rTeamDat = teamDatMap[rTeamNum]
			var bTeamDat = teamDatMap[bTeamNum]
			rTeamDat.Played++
			bTeamDat.Played++

			//teamdat.RP += int(math.Min(float64(match.RScore), float64(match.BScore)))

			rTeamDat.AutoScore += float64(match.RAuto)
			rTeamDat.TeleScore += float64(match.RTele)
			rTeamDat.Penalties += float64(match.BPen) //Use opposite team's penalty score
			if match.RScore > match.BScore {
				rTeamDat.QP += 2
				rTeamDat.Win++
			} else {
				if match.RScore == match.BScore {
					rTeamDat.QP += 1
					rTeamDat.Tie++
				} else {
					rTeamDat.Loss++
				}
			}

			bTeamDat.AutoScore += float64(match.BAuto)
			bTeamDat.TeleScore += float64(match.BTele)
			bTeamDat.Penalties += float64(match.RPen) //Use opposite team's penalty score
			if match.BScore > match.RScore {
				bTeamDat.QP += 2
				bTeamDat.Win++
			} else {
				if match.BScore == match.RScore {
					bTeamDat.QP += 1
					bTeamDat.Tie++
				} else {
					bTeamDat.Loss++
				}
			}

			teamDatMap[rTeamNum] = rTeamDat
			teamDatMap[bTeamNum] = bTeamDat
		}
	}

	for i, v := range teamDatMap {
		var tmp = teamList[i]
		tmp.TeamNumber = i

		tmp.AvgAutoScore = float64(v.AutoScore) / float64(v.Played)
		tmp.AvgTeleScore = float64(v.TeleScore) / float64(v.Played)
		tmp.AvgSumScore = tmp.AvgAutoScore + tmp.AvgTeleScore
		tmp.AvgPenScore = float64(v.Penalties) / float64(v.Played)

		tmp.QP = v.QP
		tmp.RP = v.RP
		tmp.Wins = v.Win
		tmp.Losses = v.Loss
		tmp.WinRate = 100 * (float64(v.Win) / float64(v.Win+v.Loss))

		teamList[i] = tmp
	}

	return teamList
}

func ScoreCalc(matchList []Match, teamList map[int]Team) map[int]Team {
	type TempTeamDat struct {
		OffRank, DefRank float64
		TelRank, AutRank float64

		Played int
	}
	teamDatMap := make(map[int]TempTeamDat)

	for q := 0; q < len(matchList); q++ {
		var match = matchList[q]
		var rTeam1 = teamList[matchList[q].RedTeams[0]]
		var rTeam2 = teamList[matchList[q].RedTeams[1]]
		var rTeam3 = teamList[matchList[q].RedTeams[2]]
		var bTeam1 = teamList[matchList[q].BlueTeams[0]]
		var bTeam2 = teamList[matchList[q].BlueTeams[1]]
		var bTeam3 = teamList[matchList[q].BlueTeams[2]]
		var rTeam1Dat = teamDatMap[matchList[q].RedTeams[0]]
		var rTeam2Dat = teamDatMap[matchList[q].RedTeams[1]]
		var rTeam3Dat = teamDatMap[matchList[q].RedTeams[2]]
		var bTeam1Dat = teamDatMap[matchList[q].BlueTeams[0]]
		var bTeam2Dat = teamDatMap[matchList[q].BlueTeams[1]]
		var bTeam3Dat = teamDatMap[matchList[q].BlueTeams[2]]
		rTeam1Dat.Played++
		rTeam2Dat.Played++
		rTeam3Dat.Played++
		bTeam1Dat.Played++
		bTeam2Dat.Played++
		bTeam3Dat.Played++

		redScl := winScl
		blueScl := winScl
		if match.BScore > match.RScore {
			redScl = lossScl
		}
		if match.RScore > match.BScore {
			blueScl = lossScl
		}

		rTeam1Dat.TelRank += redScl * (float64(match.RTele) - (1.0/2.0)*(rTeam2.AvgTeleScore+rTeam3.AvgTeleScore)) //Teleop
		rTeam2Dat.TelRank += redScl * (float64(match.RTele) - (1.0/2.0)*(rTeam1.AvgTeleScore+rTeam3.AvgTeleScore))
		rTeam3Dat.TelRank += redScl * (float64(match.RTele) - (1.0/2.0)*(rTeam1.AvgTeleScore+rTeam2.AvgTeleScore))
		rTeam1Dat.AutRank += redScl * (float64(match.RAuto) - (1.0/2.0)*(rTeam2.AvgAutoScore+rTeam3.AvgAutoScore)) //Autonomous
		rTeam2Dat.AutRank += redScl * (float64(match.RAuto) - (1.0/2.0)*(rTeam1.AvgAutoScore+rTeam3.AvgAutoScore))
		rTeam3Dat.AutRank += redScl * (float64(match.RAuto) - (1.0/2.0)*(rTeam1.AvgAutoScore+rTeam2.AvgAutoScore))
		rTeam1Dat.OffRank += redScl * (float64(match.RScore) - float64(match.RPen) - (1.0/2.0)*(rTeam2.AvgSumScore+rTeam3.AvgSumScore)) //Autonomous
		rTeam2Dat.OffRank += redScl * (float64(match.RScore) - float64(match.RPen) - (1.0/2.0)*(rTeam1.AvgSumScore+rTeam3.AvgSumScore))
		rTeam3Dat.OffRank += redScl * (float64(match.RScore) - float64(match.RPen) - (1.0/2.0)*(rTeam1.AvgSumScore+rTeam2.AvgSumScore))
		rTeam1Dat.DefRank += blueScl * (float64(match.BScore) - float64(match.BPen) - (1.0/2.0)*(bTeam1.AvgSumScore+bTeam2.AvgSumScore+bTeam3.AvgSumScore)) //Defensive score
		rTeam2Dat.DefRank += blueScl * (float64(match.BScore) - float64(match.BPen) - (1.0/2.0)*(bTeam1.AvgSumScore+bTeam2.AvgSumScore+bTeam3.AvgSumScore))
		rTeam3Dat.DefRank += blueScl * (float64(match.BScore) - float64(match.BPen) - (1.0/2.0)*(bTeam1.AvgSumScore+bTeam2.AvgSumScore+bTeam3.AvgSumScore))

		teamDatMap[matchList[q].RedTeams[0]] = rTeam1Dat //Writeback
		teamDatMap[matchList[q].RedTeams[1]] = rTeam2Dat
		teamDatMap[matchList[q].RedTeams[2]] = rTeam3Dat
		teamDatMap[matchList[q].BlueTeams[0]] = bTeam1Dat
		teamDatMap[matchList[q].BlueTeams[1]] = bTeam2Dat
		teamDatMap[matchList[q].BlueTeams[2]] = bTeam3Dat
	}

	for i, v := range teamDatMap {
		var tmp = teamList[i]
		tmp.TeamNumber = i
		if tmp.TeamName == "" || tmp.TeamName == "[NONAME]" {
			tmp.TeamName = "N/A"
		}

		tmp.AutRank = v.AutRank / float64(v.Played)
		tmp.TelRank = v.TelRank / float64(v.Played)
		tmp.DefRank = v.DefRank / float64(v.Played)
		tmp.OffRank = v.OffRank / float64(v.Played)

		teamList[i] = tmp
	}

	var maxOff, maxDef, maxTele, maxAuto float64
	var minOff, minDef, minTele, minAuto float64
	for _, v := range teamList {
		minOff = math.Min(minOff, v.OffRank)
		maxOff = math.Max(maxOff, v.OffRank)
		minDef = math.Min(minDef, v.DefRank)
		maxDef = math.Max(maxDef, v.DefRank)
		minAuto = math.Min(minAuto, v.AutRank)
		maxAuto = math.Max(maxAuto, v.AutRank)
		minTele = math.Min(minTele, v.TelRank)
		maxTele = math.Max(maxTele, v.TelRank)
	}

	for i, _ := range teamList {
		var tmp = teamList[i]
		tmp.Scaled.AutRank = (100.0 / (maxAuto - minAuto)) * (tmp.AutRank - minAuto)
		tmp.Scaled.TelRank = (100.0 / (maxTele - minTele)) * (tmp.TelRank - minTele)
		tmp.Scaled.OffRank = (100.0 / (maxOff - minOff)) * (tmp.OffRank - minOff)
		tmp.Scaled.DefRank = (100.0 / (maxDef - minDef)) * (tmp.DefRank - minDef)
		teamList[i] = tmp
	}

	return teamList
}

func GetMatches() {
	anaconda.SetConsumerKey("1Jvo1RB3HeYndrjMrD0vQ")
	anaconda.SetConsumerSecret("fAE6xqCzz9qHKip5sNLT7LzDCXRoIqQTOUjufp0cHEo")
	api := anaconda.NewTwitterApi("2371420178-CUHTIeXy2Pio2GhNrVM6MNmE4tiCZC3KzW2huvk", "aLKduFECY4b0h4h7exIXEa0sfPDuM8Rum5nwL4rMtaIHy")
	v := url.Values{}
	var tmpMatch Match
	v.Set("count", `100`)
	searchResult, err := api.GetSearch(`"TY Q" #FRCCASB from:frcfms`, v)
	if err != nil {
		log.Fatalln("Search Error:", err)
	}
	for _, tweet := range searchResult {
		//tweetTime, _ := tweet.CreatedAtTime()
		scores := strings.Split(tweet.Text, " ")
		for i := 0; i < len(scores); i++ {
			switch scores[i] {
			case "MC":
				i++
				tmpMatch.Number, _ = strconv.Atoi(scores[i])
				break
			case "RF":
				i++
				tmpMatch.RScore, _ = strconv.Atoi(scores[i])
				break
			case "BF":
				i++
				tmpMatch.BScore, _ = strconv.Atoi(scores[i])
				break
			case "RA":
				i++
				one, _ := strconv.Atoi(scores[i])
				i++
				two, _ := strconv.Atoi(scores[i])
				i++
				three, _ := strconv.Atoi(scores[i])
				tmpMatch.RedTeams = []int{one, two, three}
				break
			case "BA":
				i++
				one, _ := strconv.Atoi(scores[i])
				i++
				two, _ := strconv.Atoi(scores[i])
				i++
				three, _ := strconv.Atoi(scores[i])
				tmpMatch.BlueTeams = []int{one, two, three}
				break
			case "RFP":
				i++
				tmpMatch.RPen, _ = strconv.Atoi(scores[i])
				break
			case "BFP":
				i++
				tmpMatch.BPen, _ = strconv.Atoi(scores[i])
				break
			case "RHS":
				i++
				tmpMatch.RAuto, _ = strconv.Atoi(scores[i])
				break
			case "BHS":
				i++
				tmpMatch.BAuto, _ = strconv.Atoi(scores[i])
				break
			case "RTS":
				i++
				tmpMatch.RTele, _ = strconv.Atoi(scores[i])
				break
			case "BTS":
				i++
				tmpMatch.BTele, _ = strconv.Atoi(scores[i])
				break
			default:
				break
			}
		}
		Matches = append(Matches, tmpMatch)
	}
}

func Recalculate(mList []Match) map[int]Team {
	return ScoreCalc(mList, PrelimCalc(mList, UpdateTeamList(mList, make(map[int]Team))))
}

var (
	MatchData = `[{"RedTeams":[3295,4413,5121],"BlueTeams":[2375,5100,4322],"Number":87,"RScore":29,"BScore":104,"RAuto":16,"BAuto":30,"RTele":13,"BTele":54,"RPen":0,"BPen":20},{"RedTeams":[599,4556,1678],"BlueTeams":[5012,3521,4161],"Number":86,"RScore":107,"BScore":47,"RAuto":45,"BAuto":25,"RTele":62,"BTele":22,"RPen":0,"BPen":0},{"RedTeams":[701,5107,1828],"BlueTeams":[294,399,2429],"Number":85,"RScore":66,"BScore":115,"RAuto":25,"BAuto":45,"RTele":41,"BTele":70,"RPen":0,"BPen":0},{"RedTeams":[4056,1967,4322],"BlueTeams":[3473,2493,4574],"Number":84,"RScore":47,"BScore":73,"RAuto":35,"BAuto":30,"RTele":12,"BTele":43,"RPen":0,"BPen":0},{"RedTeams":[2839,4140,4201],"BlueTeams":[597,3470,4144],"Number":83,"RScore":72,"BScore":68,"RAuto":41,"BAuto":35,"RTele":31,"BTele":33,"RPen":0,"BPen":0},{"RedTeams":[4499,1160,980],"BlueTeams":[5121,4705,2339],"Number":82,"RScore":50,"BScore":13,"RAuto":26,"BAuto":10,"RTele":24,"BTele":3,"RPen":0,"BPen":0},{"RedTeams":[2659,4139,2984],"BlueTeams":[3925,2496,4114],"Number":81,"RScore":26,"BScore":21,"RAuto":15,"BAuto":10,"RTele":11,"BTele":11,"RPen":0,"BPen":0},{"RedTeams":[399,3470,4140],"BlueTeams":[294,597,2493],"Number":80,"RScore":76,"BScore":80,"RAuto":35,"BAuto":30,"RTele":41,"BTele":50,"RPen":0,"BPen":0},{"RedTeams":[4201,1160,4574],"BlueTeams":[5107,2339,3521],"Number":79,"RScore":67,"BScore":22,"RAuto":15,"BAuto":10,"RTele":52,"BTele":12,"RPen":0,"BPen":0},{"RedTeams":[599,4413,2375],"BlueTeams":[980,2429,3473],"Number":78,"RScore":117,"BScore":67,"RAuto":15,"BAuto":46,"RTele":102,"BTele":21,"RPen":0,"BPen":0},{"RedTeams":[3295,5012,5100],"BlueTeams":[4139,2496,1967],"Number":77,"RScore":32,"BScore":39,"RAuto":10,"BAuto":15,"RTele":22,"BTele":24,"RPen":0,"BPen":0},{"RedTeams":[4322,4556,5121],"BlueTeams":[1828,2659,4161],"Number":76,"RScore":42,"BScore":105,"RAuto":20,"BAuto":55,"RTele":22,"BTele":50,"RPen":0,"BPen":0},{"RedTeams":[4144,4499,4705],"BlueTeams":[1678,4114,2984],"Number":75,"RScore":45,"BScore":115,"RAuto":35,"BAuto":15,"RTele":10,"BTele":100,"RPen":0,"BPen":0},{"RedTeams":[5012,4056,2839],"BlueTeams":[4413,3925,701],"Number":74,"RScore":33,"BScore":101,"RAuto":21,"BAuto":25,"RTele":12,"BTele":76,"RPen":0,"BPen":0},{"RedTeams":[1828,1967,4201],"BlueTeams":[2496,3295,1160],"Number":73,"RScore":166,"BScore":17,"RAuto":35,"BAuto":15,"RTele":111,"BTele":2,"RPen":20,"BPen":0},{"RedTeams":[1678,4139,399],"BlueTeams":[980,4556,4140],"Number":72,"RScore":136,"BScore":72,"RAuto":45,"BAuto":21,"RTele":91,"BTele":1,"RPen":0,"BPen":50},{"RedTeams":[2984,5100,4574],"BlueTeams":[294,2839,4144],"Number":71,"RScore":33,"BScore":150,"RAuto":10,"BAuto":30,"RTele":23,"BTele":70,"RPen":0,"BPen":50},{"RedTeams":[4056,5121,5107],"BlueTeams":[4322,4114,4705],"Number":70,"RScore":13,"BScore":90,"RAuto":10,"BAuto":30,"RTele":3,"BTele":60,"RPen":0,"BPen":0},{"RedTeams":[3473,4499,2375],"BlueTeams":[3470,2429,4161],"Number":69,"RScore":105,"BScore":57,"RAuto":45,"BAuto":35,"RTele":60,"BTele":22,"RPen":0,"BPen":0},{"RedTeams":[599,3925,3521],"BlueTeams":[2493,2659,701],"Number":68,"RScore":42,"BScore":77,"RAuto":10,"BAuto":15,"RTele":32,"BTele":12,"RPen":0,"BPen":50},{"RedTeams":[399,5121,597],"BlueTeams":[2496,5100,2339],"Number":67,"RScore":105,"BScore":45,"RAuto":25,"BAuto":25,"RTele":80,"BTele":20,"RPen":0,"BPen":0},{"RedTeams":[4144,2375,4114],"BlueTeams":[3295,2839,5107],"Number":66,"RScore":37,"BScore":15,"RAuto":35,"BAuto":15,"RTele":2,"BTele":0,"RPen":0,"BPen":0},{"RedTeams":[2429,2493,4322],"BlueTeams":[980,1678,3925],"Number":65,"RScore":56,"BScore":113,"RAuto":35,"BAuto":31,"RTele":21,"BTele":82,"RPen":0,"BPen":0},{"RedTeams":[4574,1828,4056],"BlueTeams":[2659,599,4499],"Number":64,"RScore":83,"BScore":87,"RAuto":32,"BAuto":15,"RTele":51,"BTele":72,"RPen":0,"BPen":0},{"RedTeams":[701,597,4114],"BlueTeams":[3521,4499,1967],"Number":17,"RScore":128,"BScore":67,"RAuto":15,"BAuto":45,"RTele":13,"BTele":22,"RPen":100,"BPen":0},{"RedTeams":[597,5012,1160],"BlueTeams":[4705,3470,4556],"Number":63,"RScore":33,"BScore":13,"RAuto":30,"BAuto":10,"RTele":3,"BTele":3,"RPen":0,"BPen":0},{"RedTeams":[4161,2984,3473],"BlueTeams":[3521,4139,4201],"Number":62,"RScore":66,"BScore":70,"RAuto":45,"BAuto":40,"RTele":21,"BTele":30,"RPen":0,"BPen":0},{"RedTeams":[701,1967,2339],"BlueTeams":[4413,4140,294],"Number":61,"RScore":92,"BScore":85,"RAuto":30,"BAuto":35,"RTele":62,"BTele":50,"RPen":0,"BPen":0},{"RedTeams":[4322,1828,5012],"BlueTeams":[2429,1678,1160],"Number":60,"RScore":70,"BScore":101,"RAuto":30,"BAuto":30,"RTele":40,"BTele":71,"RPen":0,"BPen":0},{"RedTeams":[3521,2496,2839],"BlueTeams":[2375,4201,3470],"Number":59,"RScore":56,"BScore":180,"RAuto":5,"BAuto":30,"RTele":1,"BTele":150,"RPen":50,"BPen":0},{"RedTeams":[2339,4140,3295],"BlueTeams":[2984,5107,3925],"Number":58,"RScore":125,"BScore":37,"RAuto":15,"BAuto":15,"RTele":10,"BTele":2,"RPen":100,"BPen":20},{"RedTeams":[980,399,1967],"BlueTeams":[4056,599,4144],"Number":57,"RScore":92,"BScore":46,"RAuto":41,"BAuto":25,"RTele":51,"BTele":21,"RPen":0,"BPen":0},{"RedTeams":[4114,2493,4499],"BlueTeams":[4413,5100,4556],"Number":56,"RScore":67,"BScore":8,"RAuto":35,"BAuto":5,"RTele":32,"BTele":3,"RPen":0,"BPen":0},{"RedTeams":[5121,294,3473],"BlueTeams":[4139,597,4161],"Number":55,"RScore":68,"BScore":43,"RAuto":25,"BAuto":30,"RTele":43,"BTele":13,"RPen":0,"BPen":0},{"RedTeams":[980,701,4705],"BlueTeams":[3470,2659,4574],"Number":54,"RScore":125,"BScore":65,"RAuto":41,"BAuto":32,"RTele":84,"BTele":33,"RPen":0,"BPen":0},{"RedTeams":[2493,4201,4056],"BlueTeams":[3295,4499,4556],"Number":53,"RScore":50,"BScore":26,"RAuto":30,"BAuto":25,"RTele":20,"BTele":1,"RPen":0,"BPen":0},{"RedTeams":[2429,3521,4413],"BlueTeams":[5121,4144,4139],"Number":52,"RScore":44,"BScore":16,"RAuto":10,"BAuto":5,"RTele":14,"BTele":11,"RPen":20,"BPen":0},{"RedTeams":[5100,2839,3925],"BlueTeams":[3473,1967,5012],"Number":51,"RScore":12,"BScore":145,"RAuto":10,"BAuto":55,"RTele":2,"BTele":20,"RPen":0,"BPen":70},{"RedTeams":[294,2339,1828],"BlueTeams":[2984,4705,599],"Number":50,"RScore":110,"BScore":90,"RAuto":50,"BAuto":30,"RTele":40,"BTele":60,"RPen":20,"BPen":0},{"RedTeams":[4322,2659,1678],"BlueTeams":[4140,2375,597],"Number":49,"RScore":150,"BScore":26,"RAuto":50,"BAuto":15,"RTele":100,"BTele":11,"RPen":0,"BPen":0},{"RedTeams":[4574,399,2496],"BlueTeams":[4161,1160,4114],"Number":48,"RScore":35,"BScore":41,"RAuto":0,"BAuto":10,"RTele":35,"BTele":31,"RPen":0,"BPen":0},{"RedTeams":[980,4144,5107],"BlueTeams":[5100,4139,701],"Number":47,"RScore":25,"BScore":81,"RAuto":11,"BAuto":30,"RTele":14,"BTele":1,"RPen":0,"BPen":50},{"RedTeams":[3470,3521,1678],"BlueTeams":[5121,1967,2659],"Number":46,"RScore":101,"BScore":46,"RAuto":20,"BAuto":36,"RTele":81,"BTele":10,"RPen":0,"BPen":0},{"RedTeams":[5012,3925,2493],"BlueTeams":[4574,294,2375],"Number":45,"RScore":15,"BScore":72,"RAuto":15,"BAuto":30,"RTele":0,"BTele":42,"RPen":0,"BPen":0},{"RedTeams":[1828,4499,2496],"BlueTeams":[5107,4140,4322],"Number":44,"RScore":81,"BScore":47,"RAuto":50,"BAuto":15,"RTele":31,"BTele":32,"RPen":0,"BPen":0},{"RedTeams":[2339,4413,4161],"BlueTeams":[2984,701,4201],"Number":43,"RScore":71,"BScore":46,"RAuto":30,"BAuto":35,"RTele":41,"BTele":11,"RPen":0,"BPen":0},{"RedTeams":[2429,597,599],"BlueTeams":[4114,3295,4056],"Number":42,"RScore":47,"BScore":62,"RAuto":15,"BAuto":0,"RTele":32,"BTele":12,"RPen":0,"BPen":50},{"RedTeams":[3473,4705,1160],"BlueTeams":[4556,399,2839],"Number":41,"RScore":13,"BScore":93,"RAuto":10,"BAuto":20,"RTele":3,"BTele":73,"RPen":0,"BPen":0},{"RedTeams":[4413,3470,1967],"BlueTeams":[4322,4574,3925],"Number":40,"RScore":29,"BScore":140,"RAuto":15,"BAuto":46,"RTele":14,"BTele":94,"RPen":0,"BPen":0},{"RedTeams":[5100,3521,1828],"BlueTeams":[5012,4114,2339],"Number":39,"RScore":37,"BScore":46,"RAuto":25,"BAuto":15,"RTele":12,"BTele":31,"RPen":0,"BPen":0},{"RedTeams":[597,3473,2659],"BlueTeams":[4144,1160,2984],"Number":38,"RScore":18,"BScore":11,"RAuto":15,"BAuto":10,"RTele":3,"BTele":1,"RPen":0,"BPen":0},{"RedTeams":[2496,2429,4201],"BlueTeams":[599,5121,2493],"Number":37,"RScore":25,"BScore":80,"RAuto":25,"BAuto":30,"RTele":0,"BTele":50,"RPen":0,"BPen":0},{"RedTeams":[4499,701,399],"BlueTeams":[294,3295,1678],"Number":36,"RScore":72,"BScore":100,"RAuto":20,"BAuto":50,"RTele":52,"BTele":50,"RPen":0,"BPen":0},{"RedTeams":[4139,4705,4140],"BlueTeams":[2375,4056,4556],"Number":35,"RScore":96,"BScore":62,"RAuto":15,"BAuto":10,"RTele":31,"BTele":2,"RPen":50,"BPen":50},{"RedTeams":[1967,4161,5107],"BlueTeams":[1828,2839,980],"Number":34,"RScore":75,"BScore":51,"RAuto":55,"BAuto":41,"RTele":20,"BTele":10,"RPen":0,"BPen":0},{"RedTeams":[5012,4144,2496],"BlueTeams":[294,4499,4322],"Number":33,"RScore":26,"BScore":155,"RAuto":5,"BAuto":45,"RTele":21,"BTele":110,"RPen":0,"BPen":0},{"RedTeams":[2984,2375,3521],"BlueTeams":[4705,4056,3925],"Number":32,"RScore":62,"BScore":97,"RAuto":10,"BAuto":15,"RTele":2,"BTele":32,"RPen":50,"BPen":50},{"RedTeams":[701,5121,4161],"BlueTeams":[4140,1160,5100],"Number":31,"RScore":46,"BScore":22,"RAuto":25,"BAuto":10,"RTele":21,"BTele":12,"RPen":0,"BPen":0},{"RedTeams":[4114,4556,2659],"BlueTeams":[2493,3470,980],"Number":30,"RScore":12,"BScore":39,"RAuto":10,"BAuto":26,"RTele":2,"BTele":13,"RPen":0,"BPen":0},{"RedTeams":[1678,2339,4574],"BlueTeams":[4139,3473,3295],"Number":29,"RScore":126,"BScore":16,"RAuto":36,"BAuto":15,"RTele":90,"BTele":1,"RPen":0,"BPen":0},{"RedTeams":[399,599,5107],"BlueTeams":[597,4201,4413],"Number":28,"RScore":135,"BScore":91,"RAuto":25,"BAuto":30,"RTele":110,"BTele":11,"RPen":0,"BPen":50},{"RedTeams":[4705,4161,2839],"BlueTeams":[2659,4140,2429],"Number":27,"RScore":71,"BScore":18,"RAuto":30,"BAuto":15,"RTele":41,"BTele":3,"RPen":0,"BPen":0},{"RedTeams":[4056,4499,5100],"BlueTeams":[2339,2375,1160],"Number":26,"RScore":23,"BScore":87,"RAuto":10,"BAuto":15,"RTele":13,"BTele":22,"RPen":0,"BPen":50},{"RedTeams":[4413,4574,4114],"BlueTeams":[701,2496,1678],"Number":25,"RScore":38,"BScore":100,"RAuto":26,"BAuto":50,"RTele":12,"BTele":50,"RPen":0,"BPen":0},{"RedTeams":[4556,3473,4201],"BlueTeams":[5012,294,980],"Number":24,"RScore":23,"BScore":146,"RAuto":10,"BAuto":46,"RTele":13,"BTele":50,"RPen":0,"BPen":50},{"RedTeams":[3470,5107,4139],"BlueTeams":[2839,1967,599],"Number":23,"RScore":11,"BScore":145,"RAuto":10,"BAuto":35,"RTele":1,"BTele":60,"RPen":0,"BPen":50},{"RedTeams":[597,2984,4322],"BlueTeams":[399,3521,2493],"Number":22,"RScore":12,"BScore":82,"RAuto":10,"BAuto":10,"RTele":2,"BTele":72,"RPen":0,"BPen":0},{"RedTeams":[3295,4144,2429],"BlueTeams":[1828,5121,3925],"Number":21,"RScore":27,"BScore":105,"RAuto":25,"BAuto":45,"RTele":2,"BTele":60,"RPen":0,"BPen":0},{"RedTeams":[2496,4140,4161],"BlueTeams":[4705,1678,4413],"Number":20,"RScore":46,"BScore":120,"RAuto":26,"BAuto":40,"RTele":20,"BTele":80,"RPen":0,"BPen":0},{"RedTeams":[980,2339,2659],"BlueTeams":[5012,4201,5107],"Number":19,"RScore":50,"BScore":62,"RAuto":37,"BAuto":10,"RTele":13,"BTele":52,"RPen":0,"BPen":0},{"RedTeams":[5100,294,3470],"BlueTeams":[2984,2429,4056],"Number":18,"RScore":65,"BScore":47,"RAuto":25,"BAuto":15,"RTele":40,"BTele":32,"RPen":0,"BPen":0},{"RedTeams":[701,597,4114],"BlueTeams":[3521,4499,1967],"Number":17,"RScore":17,"BScore":44,"RAuto":15,"BAuto":10,"RTele":2,"BTele":34,"RPen":0,"BPen":0},{"RedTeams":[2375,4139,2493],"BlueTeams":[599,1160,1828],"Number":16,"RScore":25,"BScore":75,"RAuto":5,"BAuto":35,"RTele":20,"BTele":40,"RPen":0,"BPen":0},{"RedTeams":[5121,4574,2839],"BlueTeams":[4322,399,3473],"Number":15,"RScore":33,"BScore":234,"RAuto":21,"BAuto":30,"RTele":12,"BTele":134,"RPen":0,"BPen":70},{"RedTeams":[3470,3295,3925],"BlueTeams":[4161,4144,4556],"Number":14,"RScore":75,"BScore":130,"RAuto":15,"BAuto":20,"RTele":10,"BTele":10,"RPen":50,"BPen":100},{"RedTeams":[4705,2659,4201],"BlueTeams":[2429,1967,5100],"Number":13,"RScore":42,"BScore":11,"RAuto":10,"BAuto":10,"RTele":32,"BTele":1,"RPen":0,"BPen":0},{"RedTeams":[2984,4140,5121],"BlueTeams":[599,5012,4574],"Number":12,"RScore":2,"BScore":57,"RAuto":0,"BAuto":15,"RTele":2,"BTele":2,"RPen":0,"BPen":40},{"RedTeams":[3295,2375,701],"BlueTeams":[3521,597,980],"Number":11,"RScore":28,"BScore":49,"RAuto":15,"BAuto":16,"RTele":13,"BTele":13,"RPen":0,"BPen":20},{"RedTeams":[5107,1678,2493],"BlueTeams":[2839,4499,4139],"Number":10,"RScore":67,"BScore":15,"RAuto":45,"BAuto":5,"RTele":22,"BTele":10,"RPen":0,"BPen":0},{"RedTeams":[4322,4144,4413],"BlueTeams":[2339,4056,399],"Number":9,"RScore":20,"BScore":42,"RAuto":10,"BAuto":10,"RTele":10,"BTele":32,"RPen":0,"BPen":0},{"RedTeams":[1160,294,3925],"BlueTeams":[3473,1828,4114],"Number":8,"RScore":70,"BScore":142,"RAuto":30,"BAuto":10,"RTele":40,"BTele":32,"RPen":0,"BPen":100},{"RedTeams":[1967,2984,4556],"BlueTeams":[2375,5121,2496],"Number":7,"RScore":5,"BScore":10,"RAuto":5,"BAuto":10,"RTele":0,"BTele":0,"RPen":0,"BPen":0},{"RedTeams":[4161,2493,3295],"BlueTeams":[399,4705,5012],"Number":6,"RScore":35,"BScore":85,"RAuto":5,"BAuto":25,"RTele":10,"BTele":60,"RPen":20,"BPen":0},{"RedTeams":[3925,4144,3473],"BlueTeams":[4499,5107,597],"Number":5,"RScore":20,"BScore":53,"RAuto":0,"BAuto":10,"RTele":0,"BTele":23,"RPen":20,"BPen":20},{"RedTeams":[599,4114,4139],"BlueTeams":[3470,4322,701],"Number":4,"RScore":36,"BScore":86,"RAuto":5,"BAuto":45,"RTele":31,"BTele":41,"RPen":0,"BPen":0},{"RedTeams":[2429,2839,2339],"BlueTeams":[4574,3521,4556],"Number":3,"RScore":11,"BScore":6,"RAuto":10,"BAuto":5,"RTele":1,"BTele":1,"RPen":0,"BPen":0},{"RedTeams":[980,5100,4201],"BlueTeams":[1678,1828,4140],"Number":2,"RScore":31,"BScore":145,"RAuto":30,"BAuto":45,"RTele":1,"BTele":50,"RPen":0,"BPen":50},{"RedTeams":[2659,2496,294],"BlueTeams":[4413,4056,1160],"Number":1,"RScore":55,"BScore":22,"RAuto":25,"BAuto":10,"RTele":30,"BTele":12,"RPen":0,"BPen":0}]`
)

func unmarshalMatches(j []byte) []Match {
	var t []Match
	json.Unmarshal(j, &t)
	return t
}

func diagnostics() {
	//fmt.Println(MatchData)
	//fmt.Println(unmarshalMatches([]byte(MatchData)))
	//fmt.Println(`{"RedTeams":[2659,2496,294],"BlueTeams":[4413,4056,1160],"Number":1,"RScore":55,"BScore":22,"RAuto":25,"BAuto":10,"RTele":30,"BTele":12,"RPen":0,"BPen":0}`)
	//fmt.Println(unmarshalMatches([]byte(`{"RedTeams":[2659,2496,294],"BlueTeams":[4413,4056,1160],"Number":1,"RScore":55,"BScore":22,"RAuto":25,"BAuto":10,"RTele":30,"BTele":12,"RPen":0,"BPen":0}`)))
	matches := unmarshalMatches([]byte(MatchData))
	//fmt.Println(UpdateTeamList(matches, make(map[int]Team)))
	//fmt.Println(PrelimCalc(matches, UpdateTeamList(matches, make(map[int]Team))))
	fmt.Println(ScoreCalc(matches, PrelimCalc(matches, UpdateTeamList(matches, make(map[int]Team)))))
}
